﻿using System;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

public enum ConfigFileType
{
    WebConfig,
    AppConfig
}

namespace CCF20
{
    public class AppConfig : System.Configuration.AppSettingsReader
    {
        public string docName = String.Empty;
        private XmlNode node = null;
        private int _configType;
        XmlDocument cfgDoc = new XmlDocument();

        public AppConfig(string MyDocName)
        {
            docName = MyDocName;
            cfgDoc = loadConfigDoc(docName);
        }

        public int ConfigType
        {
            get
            {
                return _configType;
            }
            set
            {
                _configType = value;
            }
        }
        public string GetValue(string nodeName, string section, string attrName)
        {
            node = cfgDoc.SelectSingleNode("//" + section);

            if (node == null)
            {
                throw new System.InvalidOperationException(section + " section not found");
            }

            // XPath select setting "add" element that contains this key 	  
            XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@name='" + nodeName + "']");
            if (addElem != null)
            {
                return addElem.GetAttribute(attrName);
            }
            else
            {
                return null;
            }

        }
        public bool SetValue(string nodeName, string section, string attrName, string attrValue)
        {


            // retrieve the appSettings node 
            //node = cfgDoc.SelectSingleNode("//appSettings");
            node = cfgDoc.SelectSingleNode("//" + section);

            if (node == null)
            {
                throw new System.InvalidOperationException(section + " section not found");
            }
            // XPath select setting "add" element that contains this key 	  
            XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@name='" + nodeName + "']");
            if (addElem != null)
            {
                addElem.SetAttribute(attrName, attrValue);
            }
            // not found, so we need to add the element, key and value
            //else
            //{
            //    XmlElement entry = cfgDoc.CreateElement("add");
            //    entry.SetAttribute("key", key);
            //    entry.SetAttribute("value", attrValue);
            //    node.AppendChild(entry);
            //}
            //save it
            //saveConfigDoc(cfgDoc, docName);
            return true;

        }

        public void SaveConfigDoc()
        {
            XmlTextWriter writer = new XmlTextWriter(docName, null);
            writer.Formatting = Formatting.Indented;
            cfgDoc.WriteTo(writer);
            writer.Flush();
            writer.Close();
            return;

        }

        public bool removeElement(string elementKey)
        {
            try
            {
                // retrieve the appSettings node 
                node = cfgDoc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new System.InvalidOperationException("appSettings section not found");
                }
                // XPath select setting "add" element that contains this key to remove	  
                node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));

                SaveConfigDoc();
                return true;
            }
            catch
            {
                return false;
            }
        }


        private XmlDocument loadConfigDoc(string docName)
        {
            //// load the config file 
            //if (Convert.ToInt32(ConfigType) == Convert.ToInt32(ConfigFileType.AppConfig))
            //{

            //    docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
            //    docName += ".exe.config";
            //}
            //else
            //{
            //    docName = System.Web.HttpContext.Current.Server.MapPath("web.config");
            //}
            cfgDoc.Load(docName);
            return cfgDoc;
        }
    }
}
